package com.wkw.jkyf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkw.jkyf.common.ResultMapUtil;
import com.wkw.jkyf.pojo.Owinfo;
import com.wkw.jkyf.pojo.Problem;
import com.wkw.jkyf.service.IOwinfoService;
import com.wkw.jkyf.service.IProblemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/problem")
public class ProblemController {
    @Resource
    private IProblemService problemService;

    @RequestMapping
    public String login(){
        return "/problem";
    }

    /*
    分页查询供应商列表
     */
    @RequestMapping("/problemQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize){
        try{
            IPage<Problem> iPage = problemService.selectProblemPage(page,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtil.getHashException(e);
        }

    }
    //跳转到添加页面
    @RequestMapping(value = "/problemPage")
    public String supplierPage(){
        return "/problemPage";
    }

    //添加
    @RequestMapping(value = "/problemAdd")
    @ResponseBody
    public Object druginfoAdd(Problem problem){
        try {
            problem.setCreatetime(new Date());
            int i = problemService.addProblem(problem);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashException(e);
        }

    }

    //按id查询
    @RequestMapping(value = "/problemQueryById")
    public String owinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model) {
        Problem problem= problemService.queryProblemById(id);
        model.addAttribute("obj",problem);
        return "/problemPage";
    }

    //修改一个供应商
    @RequestMapping(value = "/problemEdit")
    @ResponseBody
    public Object owinfoEdit(Problem problem) {
        try {
            int i= problemService.editProblem(problem);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }

    // 删除一个供应商
    @RequestMapping(value = "/problemDelById")
    @ResponseBody
    public Object owinfoDelById(Integer id) {
        try {
            int i= problemService.deleteProblemById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }


}
